<script lang="ts" setup>
import { ref, onMounted } from "vue"
import { createProjectsApi, updateProjectsApi, getProjectsDetailApi } from "@/api/projects"
import { type CreateOrUpdateProjectsRequestData } from "@/api/projects/types"
import { type FormInstance, type FormRules, ElMessage } from "element-plus"
import { useRouter } from "vue-router"
import IEditor from "@/components/IEditor/index.vue"
import ProductsSelect from "./components/ProductsSelect.vue"

defineOptions({
  // 命名当前组件
  name: "ProjectsOperate"
})
const router = useRouter()
const id: string = router.currentRoute.value.params.id as string
const loading = ref<boolean>(false)

//#region 增
const DEFAULT_FORM_DATA: CreateOrUpdateProjectsRequestData = {
  id,
  name: "",
  product_id: ""
}
const formRef = ref<FormInstance | null>(null)
const formData = ref<CreateOrUpdateProjectsRequestData>(JSON.parse(JSON.stringify(DEFAULT_FORM_DATA)))
const formRules: FormRules<CreateOrUpdateProjectsRequestData> = {
  name: [{ required: true, trigger: "blur", message: "请输入项目名称" }]
}
const handleCreateOrUpdate = () => {
  formRef.value?.validate((valid: boolean, fields) => {
    if (!valid) return console.error("表单校验不通过", fields)
    loading.value = true
    const api = !formData.value.id ? createProjectsApi : updateProjectsApi
    formData.value.tag = formData.value.tag?.replace(/，/g, ",")
    formData.value.tag_en = formData.value.tag_en?.replace(/，/g, ",")
    api(formData.value)
      .then(() => {
        ElMessage.success("操作成功")
        router.push("/projects")
        resetForm()
        gotoList()
      })
      .finally(() => {
        loading.value = false
      })
  })
}
const resetForm = () => {
  formRef.value?.clearValidate()
  formData.value = JSON.parse(JSON.stringify(DEFAULT_FORM_DATA))
}

const gotoList = () => {
  router.push("/projects")
}

//#region 查
const getProjectsDetail = () => {
  if (!id) {
    return
  }
  getProjectsDetailApi(id)
    .then(async ({ data }) => {
      formData.value = data
    })
    .catch(() => {
      formData.value = DEFAULT_FORM_DATA
    })
}
onMounted(() => {
  getProjectsDetail()
})

const dialogImgVisible = ref<boolean>(false)
const file = ref<any>()
</script>

<template>
  <div class="app-container">
    <!-- 新增/修改 -->
    <el-card>
      <template #header>
        <div class="card-header">
          <h3>{{ id === undefined || "" ? "新增项目" : "修改项目" }}</h3>
        </div>
      </template>
      <el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px" label-position="left">
        <el-form-item prop="name" label="名称">
          <el-input v-model="formData.name" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="name_en" label="英文名称">
          <el-input v-model="formData.name_en" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="product_id" label="产品">
          <products-select v-model="formData.product_id" />
        </el-form-item>
        <el-form-item prop="tag" label="标签">
          <el-input v-model="formData.tag" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="tag_en" label="标签英文">
          <el-input v-model="formData.tag_en" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="introduction" label="简介">
          <IEditor v-model="formData.introduction" />
        </el-form-item>
        <el-form-item prop="introduction_en" label="简介(英文)">
          <IEditor v-model="formData.introduction_en" />
        </el-form-item>
        <el-form-item prop="detail" label="详情">
          <IEditor v-model="formData.detail" />
        </el-form-item>
        <el-form-item prop="detail_en" label="详情(英文)">
          <IEditor v-model="formData.detail_en" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="gotoList">取消</el-button>
        <el-button type="primary" @click="handleCreateOrUpdate" :loading="loading">确认</el-button>
      </template>
    </el-card>
    <el-dialog v-model="dialogImgVisible" title="图片预览" width="800" align-center>
      <img :src="file" style="width: 700px" />
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogImgVisible = false">关闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
</style>
